﻿using System;
using System.Data;
using System.Web.UI;
using System.Xml;
using AMS.Web.BaseData.Helper;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.BaseData
{
    public partial class AssetDepreciation : Page
    {
        private readonly BaseDataManager m_Acc = new BaseDataManager();
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        PermissionManager m_PermissionManger = new PermissionManager();
        private ObjXmlReader m_XmlReader;
        private string sql = "";
        private string QueryCondition
        {
            get
            {

                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }

        private string PeekType 
        {
            get
            {
                if (ViewState["PeekType"] == null)
                    return "";
                return ViewState["PeekType"].ToString();
            }
            set
            {

                ViewState["PeekType"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (Request.Cookies["Code"] != null)
            {
                string strUserId = Request.Cookies["LoginUserId"].Value;
                if (Request.QueryString["MenuId"] != null)
                {
                    string strMenuId = Request.QueryString["MenuId"].Trim();


                    if (!m_PermissionManger.CheckOperaterPermission(strMenuId, strUserId, "HandDepreciation"))
                    {
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "002", "<script>a()</script>");
                    }

                    if (!m_PermissionManger.CheckPageIsCanhandDepreciation(this))
                        btn1.Enabled = false;
                    if (!m_PermissionManger.CheckPageIsCanAutoDepreciation(this))
                        btn2.Enabled = false;
                  
                }
            }
            else
            {
                Response.Redirect("../Default.aspx");
            }
          
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            PagList.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList2);
            if (Session["Close"] != null && Session["Close"].ToString() == "Yes")
            {
                ListGridInfo.BindGrid();
                Session["Close"] = null;
            }
             if (!IsPostBack)
            {
                  m_CreateCtlHlp.SetDdlCondition(ListGridInfo, ddlCondition);
            }
        }

        private object BindList()
        {
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml";
            m_XmlReader = new ObjXmlReader(xmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/AddAssetSql");
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                sql += " " + QueryCondition;
                DataTable dt = m_Acc.GetDataTableBySql(sql);
                return dt;
            }
            return null;


        }

        private object BindList2()
        {
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml";
            m_XmlReader = new ObjXmlReader(xmlPath);
            XmlNode node;
            switch (PeekType)
            {
                case "Peek":
                     node = m_XmlReader.GetXmlNode("Setting/DepreciationQuerySql");
                    break;
                case "HistoryPeek":
                     node = m_XmlReader.GetXmlNode("Setting/DepreciationHistoryQuerySql");
                    break;
                default:
                    node = m_XmlReader.GetXmlNode("Setting/DepreciationQuerySql");
                    break;

            }
       
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                DataTable dt = m_Acc.GetDataTableBySql(sql);
                return dt;
            }
            return null;
        }
        protected void btnQuery_Click1(object sender, EventArgs e)
        {
            QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
            ListGridInfo.BindGrid();
        }

        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
            ListGridInfo.BindGrid();
        }

        protected void ListGridInfo_OnGridItemSelect(object sender, VitCode.Web.UI.PaginationGridItemSelectArgs e)
        {
            string val = e.SelectedDataKeyValue;
            var win = new Window(Page, "资产折旧")
                          {
                              Height = 260,
                              Width = 500,
                              Scrollbars = true,
                              Status = true,
                              Name = "资产折旧"
                          };
            win.ShowModalDialog("AssetDepreciationEdit.aspx?&Id=" + val, new [] { "hdCheckInfo" });
        }


        /// <summary>
        /// 自动折旧
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDepreciation_Click(object sender, EventArgs e)
        {
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml";
            m_XmlReader = new ObjXmlReader(xmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/DepreciationEditSql");
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                int access = m_Acc.ExecuteCmd(sql);
                if (access > 0)
                {
                    Alert.MessageBox("折旧成功,有:" + access + "条记录变更!");
                }
            }
        }

   

        /// <summary>
        /// 查询需要自动折旧的资产
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            PeekType = "Peek";
            PagList.BindGrid();
        }

        /// <summary>
        /// 开始自动折旧
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnStartDepreciation_Click(object sender, EventArgs e)
        {
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml";
            m_XmlReader = new ObjXmlReader(xmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/DepreciationEditSql");
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                int access = m_Acc.ExecuteCmd(sql);
                if (access > 0)
                {
                    Alert.MessageBox("折旧成功,有:" + access + "条记录变更!");
                    return;
                }
                if(access == 0)
                {
                    Alert.MessageBox("没有要折旧的资产!");
                    return;
                }
               
            }
        }

        /// <summary>
        /// 手动折旧
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       protected void btn1_Click(object sender, EventArgs e)
        {
            ListGridInfo.Visible = true;
            table2.Visible = false;
            btnQuery1.Visible = true;
            ConditionSelector1.Visible = true;
            btn1.Visible = false;
            btn2.Visible = true;
            labTip.Text = "手动折旧";
        }

        /// <summary>
        /// 自动折旧
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       protected void btn2_Click(object sender, EventArgs e)
       {
           table2.Visible = true;
           ListGridInfo.Visible = false;
           btnQuery1.Visible = false;
           ConditionSelector1.Visible = false;
           btn1.Visible = true;
            btn2.Visible = false;
           labTip.Text = "自动折旧";
       }

       protected void btnDepreciationHistory_Click(object sender, EventArgs e)
       {
           PeekType = "HistoryPeek";
           PagList.BindGrid();
       }
    }
}
